<html>
<head><meta charset="utf-8"><title>State of optimizations based on stacked borrows · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State.20of.20optimizations.20based.20on.20stacked.20borrows.html">State of optimizations based on stacked borrows</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="206353696"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State%20of%20optimizations%20based%20on%20stacked%20borrows/near/206353696" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State.20of.20optimizations.20based.20on.20stacked.20borrows.html#206353696">(Aug 08 2020 at 16:08)</a>:</h4>
<p>Hi,<br>
In this example:</p>
<div class="codehilite"><pre><span></span><code><span class="k">struct</span> <span class="nc">Hi</span><span class="p">([</span><span class="kt">usize</span><span class="p">;</span><span class="w"> </span><span class="mi">4</span><span class="p">]);</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">hi</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Hi</span><span class="p">([</span><span class="mi">1337</span><span class="p">,</span><span class="w"> </span><span class="mi">2048</span><span class="p">,</span><span class="w"> </span><span class="mi">1974</span><span class="p">,</span><span class="w"> </span><span class="mi">4235432543</span><span class="p">]);</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">hi</span><span class="p">.</span><span class="mi">0</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="n">cool</span><span class="p">(</span><span class="n">b</span><span class="p">)}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>Accessing anything other than <code>1337</code> is UB according to stacked borrows. <br>
so why aren't we optimizing the dead stores out? does this requires more MIR level optimizations or more decisions around the semantics and if rust really does adopt Stacked Borrows? </p>
<p>ASM of this: <a href="https://godbolt.org/z/q3Y5T8">https://godbolt.org/z/q3Y5T8</a></p>



<a name="206357872"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State%20of%20optimizations%20based%20on%20stacked%20borrows/near/206357872" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State.20of.20optimizations.20based.20on.20stacked.20borrows.html#206357872">(Aug 08 2020 at 18:09)</a>:</h4>
<p>I don't think there are any optimizations based on stacked borrows. It is not even the official memory model.</p>



<a name="206389315"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State%20of%20optimizations%20based%20on%20stacked%20borrows/near/206389315" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State.20of.20optimizations.20based.20on.20stacked.20borrows.html#206389315">(Aug 09 2020 at 10:36)</a>:</h4>
<p>well, there's <code>noalias</code> which we emit to LLVM for shared references (without interior mutability)</p>



<a name="206389326"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State%20of%20optimizations%20based%20on%20stacked%20borrows/near/206389326" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State.20of.20optimizations.20based.20on.20stacked.20borrows.html#206389326">(Aug 09 2020 at 10:37)</a>:</h4>
<p>that is based on the aliasing model -- though without fixing exactly what the aliasing model is, just putting a constraint on it</p>



<a name="206389761"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State%20of%20optimizations%20based%20on%20stacked%20borrows/near/206389761" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State.20of.20optimizations.20based.20on.20stacked.20borrows.html#206389761">(Aug 09 2020 at 10:48)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/State.20of.20optimizations.20based.20on.20stacked.20borrows/near/206357872">said</a>:</p>
<blockquote>
<p>I don't think there are any optimizations based on stacked borrows. It is not even the official memory model.</p>
</blockquote>
<p>Well is it the current memory model? (as opposed to being the official stable memory model) because if not then why is miri advertised as a tool to find UB and why is stdlib fixed to not violate stacked borrows?<br>
if it is the current memory model, then why not use it for optimizations? it seems weird to make something UB without utilizing it being UB.<br>
I think something like stacked borrows can potentially make rust code faster than C (at the cost of making unsafe rust harder to write correctly)</p>
<p>EDIT: I'm curious and trying to understand the state of mind, not trying to attack anyone, I think miri is awesome and stacked borrow is very interesting and useful</p>



<a name="206390078"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State%20of%20optimizations%20based%20on%20stacked%20borrows/near/206390078" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State.20of.20optimizations.20based.20on.20stacked.20borrows.html#206390078">(Aug 09 2020 at 10:59)</a>:</h4>
<blockquote>
<p>why is stdlib fixed to not violate stacked borrows?</p>
</blockquote>
<p>because I submitted PRs and the reviewers were fine with them <span aria-label="shrug" class="emoji emoji-1f937" role="img" title="shrug">:shrug:</span></p>



<a name="206390080"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State%20of%20optimizations%20based%20on%20stacked%20borrows/near/206390080" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State.20of.20optimizations.20based.20on.20stacked.20borrows.html#206390080">(Aug 09 2020 at 10:59)</a>:</h4>
<p>Stacked Borrows is an experiment, but it is an experiment supported by the lang team</p>



<a name="206390082"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State%20of%20optimizations%20based%20on%20stacked%20borrows/near/206390082" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State.20of.20optimizations.20based.20on.20stacked.20borrows.html#206390082">(Aug 09 2020 at 10:59)</a>:</h4>
<blockquote>
<p>I think something like stacked borrows can potentially make rust code faster than C (at the cost of making unsafe rust harder to write correctly)</p>
</blockquote>
<p>yes that's why we are working on this kind of model :)</p>



<a name="206390126"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State%20of%20optimizations%20based%20on%20stacked%20borrows/near/206390126" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State.20of.20optimizations.20based.20on.20stacked.20borrows.html#206390126">(Aug 09 2020 at 11:00)</a>:</h4>
<p>but before we start actually exploiting the model in optimizations, we want to be sure it's a good model -- and there are some open issues, see <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues?q=is%3Aissue+is%3Aopen+label%3AT-stacked-borrows">https://github.com/rust-lang/unsafe-code-guidelines/issues?q=is%3Aissue+is%3Aopen+label%3AT-stacked-borrows</a></p>



<a name="206390139"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State%20of%20optimizations%20based%20on%20stacked%20borrows/near/206390139" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State.20of.20optimizations.20based.20on.20stacked.20borrows.html#206390139">(Aug 09 2020 at 11:01)</a>:</h4>
<p>I used Miri as a testbed for Stacked Borrows because it was perfectly suited to do so, and nobody complained fast enough to stop me putting it upstream :) (well this was done as part of two internships with Mozilla supervised by Aaron and Niko, so it has official blessing)</p>



<a name="206390190"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State%20of%20optimizations%20based%20on%20stacked%20borrows/near/206390190" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State.20of.20optimizations.20based.20on.20stacked.20borrows.html#206390190">(Aug 09 2020 at 11:03)</a>:</h4>
<p>the other reason why no optimizations exist is that  have 0 experience in writing optimizations -- so I can't actually implement one for real, I can just analyze if a given optimization is correct. so far nobody stepped up to implementing an aliasing-based optimizations; I think there is plenty of lower-hanging fruit on the MIR optimization front.</p>



<a name="206393464"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State%20of%20optimizations%20based%20on%20stacked%20borrows/near/206393464" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/State.20of.20optimizations.20based.20on.20stacked.20borrows.html#206393464">(Aug 09 2020 at 12:42)</a>:</h4>
<p>Thank you for the detailed answer :) <br>
This is some exciting stuff, and I think I understand that right now this is mostly because almost no one is working on MIR optimizations and llvm doesn't always enable optimizations that rust can offer</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>